package android.example.graphictest.alphapng;

import android.content.Context;
import android.example.graphictest.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class DrawBitmap extends SurfaceView implements SurfaceHolder.Callback, Runnable
{
	Bitmap bitmap;
	Bitmap bitmap_bg;
	Bitmap bitmap_big;
	Paint paint;
	Canvas canvas;
	private SurfaceHolder mSurfaceHolder; 
	public static int x          = 0;
	public static int y          = 0;
	public static int x_         = 0;
	public static int y_         = 0;
	public static int h_bitmap   = 0;
	public static int w_bitmap   = 0;
	public static int dx         = 0;
	public static int dx_        = 0;
	public static int dy         = 0;
	public static int dy_        = 0;
	public static int frame      = 0;
	public static float fps      = 0;
	public static float totalFps = 0;
	public static long startTime = 0;
	public static long stopTime  = 0;
	public static int  count     = 0;
	boolean mbLoop               =  false;
	public DrawBitmap(Context context)
	{
		super(context);
		bitmap    = BitmapFactory.decodeResource(getResources(), R.drawable.image_3);
		bitmap_bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
		h_bitmap = bitmap.getHeight();
		w_bitmap = bitmap.getWidth();
		dx       = 10;
		dx_      = 10; 
		dy       = 10;
		dy_      = 10;
		mSurfaceHolder = this.getHolder();
		mSurfaceHolder.addCallback(this);
		mbLoop = true;
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStyle(Paint.Style.FILL);
		//new Thread(this).start();
		
	}
	public void Draw( )
	{
		
		if(startTime == 0)
		{
			startTime = System.currentTimeMillis();
		}
		
		canvas = mSurfaceHolder.lockCanvas();
		
//		canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
		canvas.drawBitmap(bitmap_big, 0, 0, null);

        if(x > (getWidth()-w_bitmap)|| x<0)
        {
        	dx = -dx;
        }
        if(y > (getHeight() - h_bitmap) || y<0)
        {
        	dy = -dy;
        }
        if(x_ > (getWidth()-w_bitmap)||x_<0)
        {
        	dx_ = -dx_;
        }
        if(y_ > (getHeight() - h_bitmap)||y_<0)
        {
        	dy_ = -dy_;
        }

        x  += dx; 
        x_ += dx_;
        y  += dy;
        y_ += dy_;
        {
            canvas.drawBitmap(bitmap, x, y, null);
            canvas.drawBitmap(bitmap, getWidth()- w_bitmap - x,y , null);
            canvas.drawBitmap(bitmap, x, getHeight() - h_bitmap -y, null);
            canvas.drawBitmap(bitmap, getWidth() - w_bitmap - x, getHeight()- h_bitmap - y, null);
        } 
        {
        	canvas.drawBitmap(bitmap, x_, y_, null);
            canvas.drawBitmap(bitmap, getWidth()- w_bitmap - x_, y_ , null);
            canvas.drawBitmap(bitmap, x_, getHeight() - h_bitmap - y_, null);
            canvas.drawBitmap(bitmap, getWidth() - w_bitmap - x_, getHeight()- h_bitmap - y_, null);
        }
        {
        }
		mSurfaceHolder.unlockCanvasAndPost(canvas);
        stopTime = System.currentTimeMillis();
		frame++;
		if (frame > 100)
		{
			fps       = (float)(1000*frame)/(float)(stopTime - startTime);
			frame     = 0;
			startTime = 0;
			if(count > 1 && count < 7)
			{
				totalFps += fps;
			}
			
			count++;
		}
		
	}
	
	public void run()
	   {
		   while(mbLoop)
		   {
			   try
			   {
				   //Thread.sleep(0);
			   }
			   catch(Exception e)
			   {
				   
			   }
			   synchronized(mSurfaceHolder)
			   {
				   Draw();
			   }
		   }
		  
	   } 
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		bitmap_big = Bitmap.createScaledBitmap(bitmap_bg, getWidth(), getHeight(), true);
		new Thread(this).start();
	}
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		mbLoop = false;
		
	}

}
